package cn.kattomy.shop.product.adminaction;

import java.io.File;
import java.io.IOException;
import java.util.Date;
import java.util.List;

import org.apache.commons.io.FileUtils;
import org.apache.struts2.ServletActionContext;

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ModelDriven;

import cn.kattomy.shop.categorysecond.service.CategorySecondService;
import cn.kattomy.shop.categorysecond.vo.CategorySecond;
import cn.kattomy.shop.product.service.ProductService;
import cn.kattomy.shop.product.vo.Product;
import cn.kattomy.shop.utils.PageBean;

/**
 * 后台商品管理的 Action
 * @author kattomy
 *
 */
public class AdminProductAction extends ActionSupport implements ModelDriven<Product> {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	//模型驱动使用的对象
	private Product product = new Product();
	@Override
	public Product getModel() {
		return product;
	}

	//注入商品的 Service
	private ProductService productService;
	public void setProductService(ProductService productService) {
		this.productService = productService;
	}
	
	//接收 pageNo 参数
	public Integer pageNo;
	public void setPageNo(Integer pageNo) {
		this.pageNo = pageNo;
	}
	
	//注入二级菜单的 Service
	private CategorySecondService categorySecondService;
	public void setCategorySecondService(CategorySecondService categorySecondService) {
		this.categorySecondService = categorySecondService;
	}

	//文件上传需要的参数
	private File upload;//上传的文件
	private String uploadFileName;//接收文件上传的文件名
	private String uploadContextType;//接收文件上传的文件的 MIME的类型
	public void setUpload(File upload) {
		this.upload = upload;
	}
	public void setUploadFileName(String uploadFileName) {
		this.uploadFileName = uploadFileName;
	}
	public void setUploadContextType(String uploadContextType) {
		this.uploadContextType = uploadContextType;
	}

	//带分页的查询商品的执行方法
	public String findAll(){
		//调用 Service 完成查询操作
		PageBean<Product> pageBean =  productService.findByPageNo(pageNo);
		//将数据传递到页面上
		ActionContext.getContext().getValueStack().set("pageBean", pageBean);
		return "findAll";
	}
	
	//跳转到添加商品页面的方法
	public String addPage(){
		//查询所有的二级分类
		List<CategorySecond> cslist = categorySecondService.findAll();
		ActionContext.getContext().getValueStack().set("csList", cslist);
		//页面跳转
		return "addPageSuccess";
	}
	
	//保存商品的方法
	public String save() throws IOException{
		//调用 Service 完成保存操作
		product.setPdate(new Date());
		if (upload != null) {
			//获得文件上传的磁盘绝对路径
			String realPath = ServletActionContext.getServletContext()
					.getRealPath("/products");
			//创建一个文件
			File diskFile = new File(realPath + "//" + uploadFileName);
			//文件上传
			FileUtils.copyFile(upload, diskFile);
//			product.setImage("products/" + uploadFileName);
		}
		productService.save(product);
		return "saveSuccess";
	}
	
	//删除商品的方法
	public String delete(){
		//先查询后删除
		product = productService.findByPid(product.getPid()); 
		//删除上传的图片
//		String path = product.getImage();
//		if (path != null) {
//			String realPath = ServletActionContext.getServletContext().getRealPath("/" + path);
//			File file = new File(realPath);
//			file.delete();
//		}
		//执行删除
		productService.delete(product);
		ActionContext.getContext().getValueStack().set("pageNo", pageNo);
		return "deleteSuccess";
	}
	
	//跳转到编辑商品页面的方法
	public String edit(){
		//根据商品的 id 查询该商品
		product = productService.findByPid(product.getPid());
		//查询所有的二级分类的集合
		List<CategorySecond> csList = categorySecondService.findAll();
		//将数据保存到页面
		ActionContext.getContext().getValueStack().set("csList", csList);
		//页面跳转
		return "editPageSuccess";
	}
	
	//修改商品的方法
	public String update() throws IOException{
		//文件上传
//		if (upload != null) {
			//删除原来的图片
//			String path = product.getImage();
//			File file = new File(ServletActionContext.getServletContext().getRealPath("/" + path));
//			file.delete();
//			
//			//获取文件上传磁盘的绝对路径
//			//上传新的图片
//			String realPath = ServletActionContext.getServletContext().getRealPath("/products");
//			File diskFile = new File(realPath + "//" + uploadFileName);
//			FileUtils.copyFile(upload, diskFile);
//			product.setImage("products/" + uploadFileName);
//		}
		product.setPdate(new Date());
		//修改商品到数据库
		productService.update(product);
		//页面跳转
		return "updateSuccess";
	}
	
}
